<template>
  <div class="AdendDialog">
    <el-form ref="ruleForm" :model="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-row>
        <el-col :span="24">
          <el-form-item label="角色">
            <el-select v-model="ruleForm.roleIdList" multiple placeholder="请选择">
              <el-option v-for="item in roleList" :key="item.roleId" :label="item.roleName" :value="item.roleId">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
        <el-button @click="resetForm('ruleForm')">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  export default {
    props: ["row"],
    data() {
      return {
        ruleForm: {
          roleIdList: '',
        },
        roleList: [],
      }
    },
    created: function() {
      // 获取角色树
      this.getSysRoleSelect();
    },
    methods: {
      // 获取角色树
      getSysRoleSelect: function() {
        let params = {};
        this.$server.getSysRoleSelect(params).then(res => {
          this.roleList = res.list;
          
          let roledIds = this.row.roledIds;
          if (roledIds) {
            let roleIdList = roledIds.split(',').map(Number);
            this.ruleForm.roleIdList = roleIdList;
          }
        })
      },
      // 表单提交
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let params = {
              userId: this.row.userId,
              roleIdList: this.ruleForm.roleIdList,
            }
            this.$server.editSysUser(params).then(res => {
              this.$message({
                type: "success",
                message: "分配成功!"
              });
              // 关闭弹框
              this.$emit('chang');
            })
          } else {
            // console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$emit('chang');
      },
    }
  }
</script>
<style lang="scss" scoped>
  .AdendDialog {}
</style>
